#include "serialCommand.h"
//切换状态指令
QString DORM_COMMAND = "$ACZS,DORM*hh\r\n";
QString IDLE_COMMAND = "$ACZS,IDLE*hh\r\n";
QString ACQU_COMMAND = "$ACZS,ACQU*hh\r\n" ;
QString STATE_COMMAND = "$ACZT*hh\r\n" ;//状态查询
QString command_tail = "*hh\r\n" ;
//参数查询指令
QString INFO_COMMAND = "$ACZP,I*hh\r\n";
//发射存储文件打开\关闭指令
QString LaunSaveOnCmd="$ACZP,S,LAUNSaveON*hh\r\n";
QString LaunSaveOffCmd="$ACZP,S,LAUNSaveOff*hh\r\n";
//接收文件打开\关闭指令
QString AcquSaveOnCmd="$ACZP,S,ACQUSaveON*hh\r\n";
QString AcquSaveOffCmd="$ACZP,S,ACQUSaveOff*hh\r\n";
//ACK回传打开\关闭指令
QString AckOnCmd="$ACZP,S,ACKon*hh\r\n";
QString AckOffCmd="$ACZP,S,ACKoff*hh\r\n";
QString SaveParmCmd="$ACZP,W,SaveParm*hh\r\n";
//从给定的字符串中提取出电压、电流和时间戳这些字段的值
bool getBatVTimestampFromMessage(const QString& inputString,QString& voltage, QString& current, QString& formattedDateTime,QString& SdCardGB)
{
    if (inputString.contains("M,"))//只有mcu才反馈时间
    {
        //$AZCT,M,1681280218,DORM,38.38V,47.70mA,5.15V,175.20mA*hh
        // 使用 QString 的 split() 函数将字符串按照逗号分隔成多个子字符串
        QStringList tokens = inputString.split(",");
        // 确保 tokens 中至少有 6 个元素
        if (tokens.size() < 6)
            return false;  // 如果元素个数不足，直接返回
        // 提取电压、电流和时间戳字段的值，并添加到结果列表中
        voltage = tokens.at(4).trimmed();
        current = tokens.at(5).trimmed();
        QString timestampStr = tokens.at(2).trimmed();
        // qDebug()<<"get BatV"<<voltage<<"BatC"<<current<<"RTC:"<<timestampStr;
        // 根据所需的格式设置字符串显示格式
        qint64 timestamp = timestampStr.toLongLong();//字符串表示的整数转换为 qint64 类型的变量
        QDateTime dateTime = QDateTime::fromSecsSinceEpoch(timestamp);
        formattedDateTime = dateTime.toString("yyyy/MM/dd");
        SdCardGB="0.00GB";
        return true;
    }
    else if(inputString.contains("$AZCT,P,")&&inputString.contains("GB"))
    {
        //$AZCT,P,1681280320,IDLE,38.40V,27.41mA,5.16V,79.30mA,LaunB:0B,120.03GB*hh 废弃了

        //"$AZCT,P,ACQU,36.08V,50.90mA,5.19V,81.70mA,LaunB:0B,ACQUSaveON,LAUNSaveOFF,ACKoff,ID:0,120.03GB*hh"2023-12-23新格式

        QString str=inputString;
        QStringList tokens = inputString.split(",");
        if (tokens.size() < 6)
            return false;

        voltage = tokens.at(3).trimmed();
        current = tokens.at(4).trimmed();
        int startIndex = str.lastIndexOf(",") + 1; // 最后一个逗号的下一个位置
        int endIndex = str.lastIndexOf("*hh");
        SdCardGB = str.mid(startIndex, endIndex - startIndex);
        return true;
    }
    else
    {
        return false ;
    }  // 如果不包含M或者GB直接返回

}

int checkSerialFrameType(const QString& SerialFrame)
{
    if(SerialFrame.contains("$AZCX,")||SerialFrame.contains("control symbol error"))
        return DataFrame;
    else if(SerialFrame.contains("$AZCT,"))
        return StateFrame;
    else if(SerialFrame.contains("$AZCE,F,Launching,"))
        return StateFrame;
    else if(SerialFrame.contains("$ACZP,I"))
        return InfoFrame;
    else
        return ErrorFrame;
}

void parseDataFrame(const QString& dataFrame,QString &dataLength,QString&showStr)
{
    //$AZCX,A,<SouAdd>,<DesAdd>,<EncodeMod>,<SymInv>,<FramNum>,<FramSeq>,<DataLen>,<Data>*hh<CR><LF>
    //$AZCX,A,1,2,0,1,0,60,012345678901234567890123456789012345678901234567890123456789*hh
    // 使用 QString 的 split() 函数将字符串按照逗号分隔成多个子字符串
    if(dataFrame.contains("control symbol error")){
        dataLength="0";
        showStr=dataFrame;
        return;
    }
    QStringList parts = dataFrame.split(",");
    QString startFlag = parts[0];
    QString identifier = parts[1];
    QString srcAddr = parts[2];
    QString destAddr = parts[3];
    QString encodeMod = parts[4];
    dataLength = parts[7];
    QString data = parts[8].section('*', 0, 0);  // Remove the *hh part
    QString encodeMode;
    if(encodeMod=="0"){
        encodeMode="EncodeMode:32FSK   ";
    }
    else if(encodeMod=="1"){
        encodeMode="EncodeMode:8M16FSK   ";
    }
    else if(encodeMod=="2"){
        encodeMode="EncodeMode:OFDM   ";
    }
    showStr+=encodeMode+"SrcAddr:"+srcAddr+"   DestAddr:"+destAddr+"   Data:"+data+"\r\n";
    qDebug()<<dataLength;
}
